package com.project.movie.controller.office;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.config.utils.ShiroUtils;
import com.project.movie.domain.*;
import com.project.movie.service.MovieCinemaService;
import com.project.movie.service.MovieOfficeService;
import com.project.movie.service.MovieSliceService;
import com.project.movie.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 影厅controller
 * @date 2025/02/06 07:34
 */
@Controller
@ResponseBody
@RequestMapping("office")
public class MovieOfficeController {

    @Autowired
    private MovieOfficeService movieOfficeService;
    @Autowired
    private MovieSliceService movieSliceService;

    /** 分页获取影厅 */
    @PostMapping("getMovieOfficePage")
    public Result getMovieOfficePage(@RequestBody MovieOffice movieOffice) {
        Page<MovieOffice> page = new Page<>(movieOffice.getPageNumber(),movieOffice.getPageSize());
        QueryWrapper<MovieOffice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(movieOffice.getCinemaId()),MovieOffice::getCinemaId,movieOffice.getCinemaId())
                .like(StringUtils.isNotBlank(movieOffice.getName()),MovieOffice::getName,movieOffice.getName())
                .like(StringUtils.isNotBlank(movieOffice.getTheater()),MovieOffice::getTheater,movieOffice.getTheater());
        Page<MovieOffice> movieOfficePage = movieOfficeService.page(page, queryWrapper);
        return Result.success(movieOfficePage);
    }

    @PostMapping("getMovieOfficeList")
    public Result getMovieOfficeList(@RequestBody MovieOffice movieOffice) {
        QueryWrapper<MovieOffice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(movieOffice.getCinemaId()),MovieOffice::getCinemaId,movieOffice.getCinemaId())
                .like(StringUtils.isNotBlank(movieOffice.getName()),MovieOffice::getName,movieOffice.getName())
                .like(StringUtils.isNotBlank(movieOffice.getTheater()),MovieOffice::getTheater,movieOffice.getTheater());
        List<MovieOffice> movieOfficePage = movieOfficeService.list(queryWrapper);
        return Result.success(movieOfficePage);
    }

    /** 根据id获取影厅 */
    @GetMapping("getMovieOfficeById")
    public Result getMovieOfficeById(@RequestParam("id")String id) {
        MovieOffice movieOffice = movieOfficeService.getById(id);
        return Result.success(movieOffice);
    }

    /** 保存影厅 */
    @PostMapping("saveMovieOffice")
    public Result saveMovieOffice(@RequestBody MovieOffice movieOffice) {
        boolean save = movieOfficeService.save(movieOffice);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑影厅 */
    @PostMapping("editMovieOffice")
    public Result editMovieOffice(@RequestBody MovieOffice movieOffice) {
        boolean save = movieOfficeService.updateById(movieOffice);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除影厅 */
    @GetMapping("removeMovieOffice")
    public Result removeMovieOffice(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                QueryWrapper<MovieSlice> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(MovieSlice::getOfficeId,id);
                int count = movieSliceService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("影厅已排片,无法删除");
                }
                movieOfficeService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("影厅id不能为空！");
        }
    }

}